package com.boarsoft.boar.config.client.listener;

import java.lang.reflect.Field;
import java.util.Map;

import org.apache.commons.beanutils.ConvertUtils;

import com.boarsoft.config.core.ConfigVarListener;
import com.boarsoft.config.manager.PropConfigManager;

/**
 * 监听变量推送，将变量值写入配置对象的属性
 * 
 * @author Mac_J
 *
 */
public class ConfigVarChangeListener implements ConfigVarListener {

	@Override
	public void onChange(String group, Map<String, String> items) {
		for (String k : items.keySet()) {
			Object obj = PropConfigManager.getResource(group);
			try {
				Field f = obj.getClass().getDeclaredField(k);
				f.setAccessible(true);
				f.set(obj, ConvertUtils.convert(items.get(k), f.getType()));
			} catch (NoSuchFieldException | IllegalAccessException e) {
				throw new RuntimeException(e);
			}
		}
	}
}
